package com.yycx.module.bus.provider.handler.refund;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 待返款列表（已支付开票税金）
 *
 * @author zyf
 */
@Component("paidInvoicePaymentPageListHandler")
public class PaidInvoicePaymentPageListHandler implements PageInterceptor<BusPayment> {
    @Autowired
    private TaskService taskService;
    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {

        cq.select(BusPayment.class, "*");
        cq.select(BusSupplier.class, "supplierName");
        cq.select("fk.paymentId fankuanId","fk.paymentState fkPaymentState","fk.processInstanceId fkProcessInstanceId","fk.amountDate fkAmountDate","fk.paymentNo fkPaymentNo");
        cq.select(BusInvoiceunitBank.class, "bankName");
        cq.addSelect(BusInvoiceunit.class, "invoiceunitName");
        cq.addSelect(BaseUser.class, "nickName payUserName");
        cq.eq(BusPayment.class, "supplierId");
        cq.like(BusPayment.class, "paymentNo");
        //查询未申请返款和已申请未审批的
        cq.and(e->e.isNull("fk.paymentId").or().ne("fk.paymentState",CommonConstants.INT_2));
        cq.isNull("fk.parentId");
        //税金已支付
        cq.eq(BusPayment.class,"paymentState", CommonConstants.INT_2);
        cq.eq(BusPayment.class, "amountType", AmountTypeEnum.Invoice.getCode());
        cq.eq(true,"payment.companyId", OpenHelper.getCompanyId());
        // cq.having("sum(fk.amount)<max(payment.supplierRetable)");
        cq.createJoin(BusPayment.class).setJoinAlias("fk").setJoinField("parentId");
        cq.createJoin(BusSupplier.class);
        cq.createJoin(BusInvoiceunit.class);
        cq.createJoin(BusInvoiceunitBank.class);
        cq.createJoin(BaseUser.class).setMainField("payUserId");
        cq.orderByDesc("payment.paymentNo");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("fkProcessInstanceId");
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }
}
